package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.file.MinIoTemplate;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmMaterialDto;
import com.heima.model.wemedia.pojos.WmMaterial;

import com.heima.wemedia.mapper.WmMaterialMapper;
import com.heima.wemedia.service.WmMaterialService;
import com.heima.wemedia.threadLocal.WmUserThreadLocal;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.multipart.MultipartFile;


import java.io.IOException;
import java.io.InputStream;
import java.util.Date;


@Slf4j
@Service
@Transactional
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial> implements WmMaterialService {

    @Autowired
    private MinIoTemplate minIoTemplate;
    @Autowired
    private WmMaterialMapper wmMaterialMapper;

    @Override
    public ResponseResult upload(MultipartFile multipartFile)  throws IOException {

        //获取图片输入流
        InputStream inputStream = multipartFile.getInputStream();

        //根据/分调
        String contentType = multipartFile.getContentType();
        //如果为空，报错
        if (StringUtils.isEmpty(contentType)){
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
        }

        String[] split = contentType.split("/");
        String s = split[1];
        String fileName = System.currentTimeMillis() + "." + s;
        //将图片上传到minio
        String url = minIoTemplate.uploadFile("",fileName,inputStream,"image");


        //将url上传到表中
        WmMaterial material = new WmMaterial();
        material.setCreatedTime(new Date());
        material.setIsCollection((short) 0);
        material.setType((short) 0);
        material.setUrl(url);
        material.setUserId(WmUserThreadLocal.get().getId());
        int result = wmMaterialMapper.insert(material);
        if (result < 1){
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
        }


        return ResponseResult.okResult(material);
    }



    @Override
    public PageResponseResult findList(WmMaterialDto dto) {
        //判断是否为空
        if (dto == null){
            return new PageResponseResult(0,0,0);
        }
        LambdaQueryWrapper<WmMaterial> wrapper =new LambdaQueryWrapper<>();
        //获取用户

       wrapper.eq(dto.getIsCollection() == 1 ,WmMaterial::getIsCollection ,dto.getIsCollection() );
       wrapper.orderByDesc(WmMaterial::getCreatedTime);
        Integer dtoPage = dto.getPage();
        Integer size = dto.getSize();
        Page<WmMaterial> page = new Page<>(dtoPage,size);
        Page<WmMaterial> pageResrlt = wmMaterialMapper.selectPage(page, wrapper);

        Long total = pageResrlt.getTotal();
        PageResponseResult pageResponseResult =new PageResponseResult(dtoPage,size,total.intValue());
        pageResponseResult.setData(pageResrlt.getRecords());


        return pageResponseResult;
    }
}
